package co.edu.uniandes.moviles.rollerjudge;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import co.edu.uniandes.moviles.rollerjudge.logica.Deportista;
import co.edu.uniandes.moviles.rollerjudge.logica.Equipo;
import co.edu.uniandes.moviles.rollerjudge.logica.PruebaGrupal;
import co.edu.uniandes.moviles.rollerjudge.logica.PruebaIndividual;
import co.edu.uniandes.moviles.rollerjudge.logica.RollerJudge;

public class CrearRegistroActivity extends Activity{

	protected static final int DATE_DIALOG_ID = 999;
	private String depor;
	private String tipo;


	private Spinner spinner1, spinner2;
	private Button btnSummit;
	private String grupo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		RollerJudge roller = RollerJudge.darInstancia();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crear_registro);

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		List<String> list2 = new ArrayList<String>();
		list2.add("FALTA");
		list2.add("LLEGADA");
		list2.add("ELIMINACION");

		ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list2);
		dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(dataAdapter2);

		if(roller.getPruebaGrupalActiva()!=null)
		{spinner2 = (Spinner) findViewById(R.id.spinner2);
		List<String> list = new ArrayList<String>();
		
		PruebaGrupal prueba = roller.getPruebaGrupalActiva();
		
		for(int i=0;i<prueba.getParticipantes().size();i++)
		{
			Equipo eq= prueba.getParticipantes().get(i);
			list.add(eq.getNombre());

		}

		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner2.setAdapter(dataAdapter);


		}
		else{
			spinner2 = (Spinner) findViewById(R.id.spinner2);
			List<String> list = new ArrayList<String>();
			
			PruebaIndividual prueba = roller.getPruebaIndividualActiva();
			for(int i=0;i<prueba.getParticipantes().size();i++)
			{
				Deportista eq= prueba.getParticipantes().get(i);
				list.add(eq.getNombre());

			}

			ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, list);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner2.setAdapter(dataAdapter);

		}


	}
	
	public void crearNotaDeVoz(View view){
		Intent intent = new Intent(this, GrabarNotaVozActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}



	@SuppressWarnings("deprecation")
	public void showDialog(String title, String message, final boolean finishAfterClose) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton("Aceptar", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(final DialogInterface dialog, final int which) {				
				if(finishAfterClose){
					finish();
				}
			}
		});
		alertDialog.show();
	}

	public void registrar(View view){
		


		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner2 = (Spinner) findViewById(R.id.spinner2);

		tipo = String.valueOf(spinner1.getSelectedItem());
		depor = String.valueOf(spinner2.getSelectedItem());

		try {

			System.out.println("nombre:" + depor +" tipo" + tipo);
			RollerJudge.darInstancia().realizarRegistro(depor, tipo);
			finish();

		} catch(Exception e) {
			
				showDialog("Mensaje", e.getMessage(), false);
		}	

	}

}


